﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string GCM_Add(GCM model ,int ACID);


[OperationContract]
string GCM_Update(GCM model ,int ACID);


[OperationContract]
string GCMBygcmid_Delete(int gcmid,int ACID);


[OperationContract]
GCM GCMBygcmid_Get(int gcmid,int ACID );


[OperationContract]
List<GCM> GCM_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加GCM表数据
/// <summary>
/// 添加GCM
/// </summary>
/// <param name="model">GCM实体</param>
/// <returns>空或错误</returns>
public string GCM_Add(GCM model,int ACID)
{
if(model.gcmid==null )
{
return "『核算方法ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.GCM.Count(p=>p.gcmid==model.gcmid)>0)
{
return "主键已存在！";
}
ctx.GCM.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加GCM表数据事物用重构
/// <summary>
/// 添加GCM事物用重构
/// </summary>
/// <param name="model">GCM实体</param>
/// <returns>空或错误</returns>
private string GCM_Add(GCM model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.gcmid==null )
{
return "『核算方法ID』不能为空！";
}
if(ctx.GCM.Count(p=>p.gcmid==model.gcmid)>0)
{
return "主键已存在！";
}
ctx.GCM.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改GCM表数据
/// <summary>
/// 修改GCM
/// </summary>
/// <param name="model">GCM实体</param>
/// <returns>空或错误</returns>
public string GCM_Update(GCM model,int ACID)
{
if(model.gcmid==null )
{
return "『核算方法ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.GCM.Count(p=>p.gcmid==model.gcmid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
GCM GCMmodel=ctx.GCM.Single(p => p.gcmid==model.gcmid);
if(model.gcmid!=null )
{
GCMmodel.gcmid=model.gcmid;
}
if(model.isubid!=null )
{
GCMmodel.isubid=model.isubid;
}
if(model.csubid!=null )
{
GCMmodel.csubid=model.csubid;
}
if(model.ssubid!=null )
{
GCMmodel.ssubid=model.ssubid;
}
if(model.psubid!=null )
{
GCMmodel.psubid=model.psubid;
}
if(model.adjsubid!=null )
{
GCMmodel.adjsubid=model.adjsubid;
}
if(!string.IsNullOrEmpty(model.name))
{
GCMmodel.name=model.name;
}
if(model.kind!=null )
{
GCMmodel.kind=model.kind;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除GCM表Bygcmid数据
/// <summary>
/// 删除GCMBygcmid
/// </summary>
/// <param name="gcmid">核算方法ID</param>
/// <returns>空或错误</returns>
public string GCMBygcmid_Delete(int gcmid,int ACID)
{
if(gcmid==null )
{
return "『核算方法ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.GCM.Count(p=>p.gcmid==gcmid)!=1)
{
return null;
}
GCM model=ctx.GCM.Single(p => p.gcmid== gcmid);
ctx.GCM.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除GCM表Bygcmid数据[事物用重构]
/// <summary>
/// 删除GCMBygcmid[事物用重构]
/// </summary>
/// <param name="gcmid">核算方法ID</param>
/// <returns>空或错误</returns>
private string GCMBygcmid_Delete(int gcmid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(gcmid==null )
{
return "『核算方法ID』不能为空！";
}
if(ctx.GCM.Count(p=>p.gcmid==gcmid)!=1)
{
return null;
}
GCM model=ctx.GCM.Single(p => p.gcmid== gcmid);
ctx.GCM.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取GCM表Bygcmid数据
/// <summary>
/// 获取GCMBygcmid
/// </summary>
/// <param name="gcmid">核算方法ID</param>
/// <returns>当前表</returns>
public GCM GCMBygcmid_Get(int gcmid,int ACID)
{
if(gcmid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.GCM.Count(p=>p.gcmid==gcmid)!=1)
{
return null;
}
return ctx.GCM.SingleOrDefault(p=>p.gcmid==gcmid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取GCM表数据
/// <summary>
/// 获取GCM
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<GCM></returns>
public List<GCM> GCM_Get(Expression<Func<GCM, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.GCM.ToList();
}
 else 
{
return ctx.GCM.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.GCM.Where(predicate).ToList();
}
 else 
{
return ctx.GCM.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取GCM表数据
/// <summary>
/// 获取GCM
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<GCM></returns>
public List<GCM> GCM_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return GCM_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取GCM表数据计数
/// <summary>
/// 获取GCM表数据计数
/// </summary>
/// <returns>List<GCM></returns>
public int GCM_GetCount(Expression<Func<GCM, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.GCM.Count();

}
 else
{
return ctx.GCM.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取GCM分页表数据
/// <summary>
/// 获取GCM分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<GCM></returns>
public List<GCM> GCM_Getpage(int pageIndex, int pageSize,Expression<Func<GCM, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.GCM.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.GCM.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.GCM.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.GCM.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改GCM表数据
/// <summary>
/// 批量修改GCM
/// </summary>
/// <param name="predicate">Expression<Func<GCM, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<GCM,GCM>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int GCM_batchUpdate(Expression<Func<GCM, bool>> predicate,Expression<Func<GCM, GCM>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.GCM.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改GCM表数据事物用重构
/// <summary>
/// 批量修改GCM事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<GCM, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<GCM,GCM>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int GCM_batchUpdate(Expression<Func<GCM, bool>> predicate,Expression<Func<GCM, GCM>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.GCM.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除GCM表数据
/// <summary>
/// 批量删除GCM
/// </summary>
/// <param name="predicate">Expression<Func<GCM, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int GCM_batchDel(Expression<Func<GCM, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.GCM.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除GCM表数据事物用重构
/// <summary>
/// 批量删除GCM事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<GCM, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int GCM_batchDel(Expression<Func<GCM, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.GCM.Delete(predicate);
return ok;
}
#endregion


}
}

